var app = getApp();
var ddapi = require('../../utils/md5.js');
Page({
  data: {
    selectPerson: true,
    firstPerson: '请选择店铺',
    fistId: 0,
    selectArea: false,
    selectPerson_1: true,
    firstPerson_1: '请选择商品',
    fistId_1: 0,
    selectArea_1: false,
    shop : [],
    product: [],
    td: '0',
  },
  //点击选择类型
  clickPerson: function () {
    var that = this

    if (that.data.shop.length == 0) {
      wx.showModal({
        title: "提示",
        content: "暂无店铺",
        showCancel: false
      })
    } else {
      var selectPerson = that.data.selectPerson;
      if (selectPerson == true) {
        that.setData({
          selectArea: true,
          selectPerson: false,
        })
      } else {
        that.setData({
          selectArea: false,
          selectPerson: true,
        })
      }
    }
  },
  //点击切换
  mySelect: function (e) {
    console.log(e.currentTarget.dataset['id']);
    var that = this
    that.setData({
      firstPerson: e.target.dataset.me,
      fistId: e.currentTarget.dataset['id'],
      selectPerson: true,
      selectArea: false,
    })
  },
  //点击选择类型
  clickPerson_1: function () {
    var that = this

    if (that.data.fistId == 0) {
      wx.showModal({
        title: "提示",
        content: "请先选择店铺",
        showCancel: false
      })
    } else {
      that.loadProduct();
    }
  },
  //点击切换
  mySelect_1: function (e) {
  var that = this
    this.setData({
      firstPerson_1: e.target.dataset.me,
      fistId_1: e.currentTarget.dataset['id'],
      selectPerson_1: true,
      selectArea_1: false,
    })
  },
  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    var that = this
    that.loadData();
  },
  loadData: function () {
    var that = this
    var openid = wx.getStorageSync('openid');
    var user_id = wx.getStorageSync('user_id');
    var service = 'User.GetMyShop';
    var shop_id = 0;
    var sign = ddapi.md5(openid + service + shop_id + user_id);
    wx.request({
      url: app.globalData.apiUrl + '?service=User.GetMyShop',
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        open_id: openid,
        user_id: user_id,
        shop_id: shop_id
      },

      success: function (res) {
        console.log(res)
        var info = res.data.data.info
        that.setData({
          shop: info
        });
      }
    })
  },
  loadProduct: function () {
    var that = this
    var service = 'Shop.GetShopProduct';
    var shop_id = that.data.fistId;
    var status = '1';
    var sign = ddapi.md5(service + shop_id + status);
    wx.request({
      url: app.globalData.apiUrl + '?service=Shop.GetShopProduct',
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        shop_id: shop_id,
        status: status
      },

      success: function (res) {
        console.log(res)
        var info = res.data.data.info
        that.setData({
          product: info
        });

        var selectPerson_1 = that.data.selectPerson_1;
        if (selectPerson_1 == true) {
          that.setData({
            selectArea_1: true,
            selectPerson_1: false,
          })
        } else {
          that.setData({
            selectArea_1: false,
            selectPerson_1: true,
          })
        }
      }
    })
  },
  fbproduct: function (e) {
    var that = this
    if (that.data.fistId == 0) {
      wx.showModal({
        title: "提示",
        content: "请先选择店铺",
        showCancel: false
      })
    } else if (that.data.fistId_1 == 0) {
      wx.showModal({
        title: "提示",
        content: "请先选择商品",
        showCancel: false
      })
    } else if (that.data.td == '0') {
      wx.showModal({
        title: "提示",
        content: "请先输入提点",
        showCancel: false
      })
    } else {
      that.sendTd();
    }
  },
  tdInput: function(e) {
    var that = this
    that.setData({
      td: e.detail.value
    })
  },
  sendTd: function () {
    var that = this
    var service = 'Product.MakeGongxiang';
    var openid = wx.getStorageSync('openid');
    var user_id = wx.getStorageSync('user_id');
    var shop_id = that.data.fistId;
    var product_id = that.data.fistId_1;
    var gongxiang_tidian = that.data.td;
    var sign = ddapi.md5(gongxiang_tidian + openid + product_id + service + shop_id + user_id);
    wx.request({
      url: app.globalData.apiUrl + '?service=Product.MakeGongxiang',
      header: {
        'Content-Type': 'json'
      },
      data: {
        sign: sign,
        shop_id: shop_id,
        gongxiang_tidian: gongxiang_tidian,
        product_id: product_id,
        openid: openid,
        user_id: user_id
      },

      success: function (res) {
        console.log(res)
        var code = res.data.data.code;
        if (code == 0) {
          wx.showModal({
            title: "提示",
            content: "发布成功",
            showCancel: false
          })
        } else {
          wx.showModal({
            title: "提示",
            content: "发布失败",
            showCancel: false
          })
        }
      }
    })
  }
})